import _ from "lodash";
import { typeClassName } from "@/pages/config/classNameMap";
import { mapMutations, mapActions, mapState } from "vuex";
const requireComponent = require.context("./", true, /[A-Z]\w+\.(vue)$/);
const comp = {};
requireComponent.keys().forEach((fileName) => {
  if (fileName != "./index.vue") {
    const componentConfig = requireComponent(fileName);
    const name = componentConfig.default.name;
    const componentName = _.camelCase(
      name
        .split("/")
        .pop()
        .replace(/\.\w+$/, "")
    );
    comp[componentName] = componentConfig.default || componentConfig;
  }
});
Object.keys(comp).map((key) => {
  comp[key].components = comp;
});
export default {
  components: comp,
  provide() {
    return {
      anc: this,
    };
  },
  computed: {},
  watch: {},
  created() {
    Object.keys(this.models).map((k) => {
      this.models[k].clz = this.$modelObject.create(this.models[k].modelType);
      this.models[k].clzs = this.$modelObject.create(
        this.models[k].modelType + "Collection"
      );
    });
  },
  mounted() {
    this.watchs.map(item=>{
      this.$watch(item.target,item.fn,{deep:item.deep})
    })
    this.$nextTick(() => {
      this.unwatch = this.$watch('vms', (newValue, oldValue) => {
        let flag = true;
        Object.keys(comp).map((k) => {
          if (!Object.keys(this.vms).includes(k)) {
            flag = false;
          }
        });
        this.completeLoad = flag;
      });
      this.init();
      if (process.env.NODE_ENV === "development") {
        console.log({ me: this, vms: this.vms, completeLoad:this.completeLoad });
      }
    });
  },
  destroyed(){
    this.unwatch();
  },
  data() {
    return {
      vms: {}, //页面组件vue实例
      completeLoad: false,
      me:this,
      watchs:[],
      models: {
        //模型
        abc: typeClassName["abc"],
      },
      dataSet: {
        //数据集
        data:[]
      }
    };
  },
  methods: {
    init(){

    },
    loadPage(){

    },
    handleCreate() {
      this._setVmValue("createModal", "config.show", true);
    },
    createModalOk(loading) {
      this._$validateAll([this.vms.createView], (valid) => {
        if (valid) {
          this._setVmValue("createModal", "config.show", false);
        } else {
          this.$Message.info("请填写必填内容");
        }
      });
    },
    handleEdit(row) {
      this._setDataSet("", row)
      this._setVmValue("editModal", "config.show", true);
    },
    editModalOk() {
      this._$validateAll([this.vms.editView], (valid) => {
        if (valid) {
          this._setVmValue("editModal", "config.show", false);
        } else {
          this.$Message.info("请填写必填内容");
        }
      });
    },
    handleDelete(row) {
      this._$deleteConfirm(() => {
        
      });
    },
    /****************工具***************/
    _getModelFields(name, ...args) {
      let map = {};
      args.map((key) => {
        map[key] = this.models[name][key];
      });
      return map;
    },
    _getModelRecord(name) {
      return this.models[name].clz.record;
    },
    _setVmValue(name, key, value) {
      _.set(this.vms[name], key, value);
    },
    _getModelTpl(name){
      return this.$modelObject.getTpl(this.models[name].modelType);
    },
    _setDataSet(name, data) {
      this.dataSet[name] = data;
    },
    _modalShow(name){
      _.set(this.vms[name], 'config.show', true);
    },
    _modalClose(name){
      _.set(this.vms[name], 'config.show', false);
      _.set(this.vms[name], 'config.loading', false);
    },
    _tableLoading(name){
      _.set(this.vms[name], 'loading', true);
    },
    _tableUnLoading(name){
      _.set(this.vms[name], 'loading', false);
    },
    _resetForm(name){
      this.vms[name].resetFields();
    }
  },
};
